package com.ruoyi.system.controller;


import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.DevPrastructDef;
import com.ruoyi.system.service.DevPrastructDefService;
import com.ruoyi.system.util.MinioUtil;
import io.minio.errors.*;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * 通用设备参数
 */
@RestController
@RequestMapping("/def")
public class DevPrastructDefController extends BaseController {
    @Autowired
    private DevPrastructDefService devPrastructDefService;
    @Autowired
    private MinioUtil minioUtil;

    /**
     * 根据设备类型查看相关参数
     * @param devPrastructDef
     * @return
     */
    @GetMapping("/ListDef")
    public TableDataInfo listDef(DevPrastructDef devPrastructDef){
        List<DevPrastructDef> listDef=devPrastructDefService.ListDef(devPrastructDef);
        return getDataTable(listDef);
    }
    /**
     * 查询所有设备参数
     */
    @GetMapping("/list")
    public TableDataInfo list(DevPrastructDef devPrastructDef){
        startPage();
        List<DevPrastructDef>list=devPrastructDefService.list(devPrastructDef);
        return getDataTable(list);
    }
    String url="";
    /**
     * 接收图片
     */
    @PostMapping("/img")
    public String img(MultipartFile file) throws ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, IOException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        if (file!=null){
            url=minioUtil.minioUpload(file);
        }
        return url;
    }
    /**
     * 新增设备参数
     */
    @PostMapping("/insterDef")
    public AjaxResult insterDef(@RequestBody(required = false) DevPrastructDef devPrastructDef){
        DevPrastructDef paraNo=devPrastructDefService.selectno1(devPrastructDef.getParaNo1());
        if (paraNo!=null){
            AjaxResult ajaxResult=AjaxResult.error("序号已存在，不能重复输入");
            return ajaxResult;
        }
        return toAjax(devPrastructDefService.insterDef(devPrastructDef));
    }
    /**
     * 修改设备参数
     */
    @PutMapping("/updateDef")
    public AjaxResult updateDef(@RequestBody(required = false) DevPrastructDef devPrastructDef){
        return toAjax(devPrastructDefService.updateDef(devPrastructDef));
    }
    /**
     * 删除设备参数
     */
    @DeleteMapping("/deleteDef")
    public AjaxResult deleteDef(Integer id){
        return toAjax(devPrastructDefService.deleteDef(id));
    }

}
